#include "util.h"

// Double Buffer
BITMAP   *screenAlias  = NULL;
BITMAP   *doubleBuffer = NULL;

// Die Timer Variablen
// -------------------------------------------------------
volatile int timerCounter = 0;
static void timerCounterUpdater() {
    timerCounter++;    
}
END_OF_STATIC_FUNCTION(timerCounterUpdater);


void fatalError(char *str) {
    allegro_message(str);
    exit(0);
}

int setGfxMode(int mode, int width, int height) {
    static int colorDepths[] = { 16, 24, 15, 32};
    int a;  
    
    for (a=0; a < 4; a++) {
        set_color_depth(colorDepths[a]);
        if (set_gfx_mode(mode, width, height, 0, 0) >= 0) {
            return TRUE;
        }
    }
    return FALSE;
}

void playSound(SAMPLE *sample, int vol) {
    int freq= rnd(100) + 950;
    
    vol *= 5+(rnd(5)+1);
    vol /= 10;
    play_sample(sample, vol, 128, freq, 0);
}


void show() {
    blit(doubleBuffer, screen, 0, 0, 0, 0, doubleBuffer->w, doubleBuffer->h);
}

int rnd( int n ){
    return (int)( n * ( (double)rand() / (double)(RAND_MAX + 1.0 ) ) );
}

void init(int w, int h, int fps, bool fullscreen) {
    srand(time(NULL));
    
    // Allegro und Grafik, Timer, Sound und Eingabemedien
    // initilaisieren
    allegro_init();

    if (!fullscreen || !setGfxMode(GFX_AUTODETECT_FULLSCREEN, w, h)) {
        if (!setGfxMode(GFX_AUTODETECT_WINDOWED, w, h)) {
            fatalError("Unable to set a graphics mode");
        }
    }
    install_timer();
    

    /* Den DoubleBuffer erstellen */
    screenAlias  = create_video_bitmap(SCREEN_W, SCREEN_H);
    doubleBuffer = create_video_bitmap(SCREEN_W, SCREEN_H);
    if (!doubleBuffer) {
        if (screenAlias != NULL) {
            destroy_bitmap(screenAlias);            
        }
        doubleBuffer = create_system_bitmap(SCREEN_W, SCREEN_H);
        if (!doubleBuffer) {
            fatalError("Unable to create double buffer");
        }
    }
    
    vsync();
    /* Timer Funktionen und Variablen locken */
    LOCK_FUNCTION(timerCounterUpdater);
    LOCK_VARIABLE(timerCounter);
    /* Die logische Framerate setzen */    
    install_int_ex(timerCounterUpdater, BPS_TO_TIMER(fps));
    
    // Allegro Logo anzeigen
    BITMAP *logo = load_bitmap("allegro.tga", NULL);
    if (logo) {
        blit(logo, screen, 0, 0, (SCREEN_W-logo->w)/2, (SCREEN_H-logo->h)/2, logo->w, logo->h);
    }
    destroy_bitmap(logo);
    if (fullscreen) {
        rest(1000);
    }
    rest(1000);
    install_keyboard();
    install_joystick(JOY_TYPE_AUTODETECT);
    install_mouse();
    install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
    
    
    // Den Rahmen um den Text verschwinden lassen
    text_mode(-1);
    // ASCII Zeichen benutzen
    set_uformat(U_ASCII);
    
    
    
}

// Gibt die in init() angelegten Resourcen
// wieder frei
void done() {
    destroy_bitmap(doubleBuffer);
    if (screenAlias != NULL) {
        destroy_bitmap(screenAlias);
    }
}

void syncTimer(volatile int * timer) {
    int value = *timer;
    while (*timer == value);
}
